package com.hunan.api.catv.service.biz.activition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.entity.po.ActivationQrCode;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.member.MemberService;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/9/8 0008
 * @package com.hunan.api.catv.service.biz.activition
 * @company 湖南有线
 */
@Service
public class ActivationBiz extends ResultService {
    @Autowired
    MemberService memberService;

    @Autowired
    RedisUtils redisUtils;


    public Object verfication(Integer memberId, Integer type, String code) {
        Object o = redisUtils.hget("promise_my_story_film_activation", String.valueOf(memberId));
        if (!ObjectUtils.isEmpty(o)) {
            return buildOK(true);
        }
        if (!headCheck(code)) {
            return buildFail("校验失败，无效验证码！");
        }
        ActivationQrCode activationQrCode = new ActivationQrCode().selectOne(new QueryWrapper<ActivationQrCode>().eq(ActivationQrCode.TYPE, type).eq(ActivationQrCode.CODE, code).eq(ActivationQrCode.DELETED, 1));
        if (!ObjectUtils.isEmpty(activationQrCode)) {
            if (ObjectUtils.isEmpty(activationQrCode.getMemberId()) && activationQrCode.getState().equals(0)) {
                activationQrCode.setMemberId(memberId);
                activationQrCode.setState(1);
                activationQrCode.setUpdateTime(new Date());
                boolean updateById = activationQrCode.updateById();
                if (updateById) {
                    redisUtils.hset("promise_my_story_film_activation", String.valueOf(memberId), code);
                    redisUtils.incr("promise_my_story_film_view_num", 1L);
                    return buildOK(true);
                }
                return buildFail("校验失败，请重试！");
            }
            if (activationQrCode.getMemberId().equals(memberId)) {
                return buildOK(true);
            }
            MemberVo memberVo = memberService.getById(activationQrCode.getMemberId());
            String phone = memberVo.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
            return buildFail("很遗憾，此二维码已被" + phone + "用户绑定激活！");
        }
        return buildFail("校验失败，无效验证码！");
    }

    private boolean headCheck(String code) {
        int i = 0;
        int j = 0;
        int length = code.length();
        try {
            if (length < 12) {
                return false;
            }
            if (!checkisHex(code)) {
                return false;
            }
            String s1 = code.substring(3, 9);
            i = Integer.parseInt(s1, 16);
            String s2 = code.substring(length - 9, length - 3);
            j = Integer.parseInt(s2, 16);
        } catch (Exception e) {
            log.error("{}", e);
            return false;
        }
        return i + j == 12280930;
    }

    private boolean checkisHex(String str) {
        for (int i = 0; i < str.length(); i++) {
            if ((Character.digit(str.charAt(i), 16) < 0)) {
                return false;
            }
        }
        return true;
    }
}
